package cn.iocoder.yudao.module.shop.convert.member;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserRespVO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.group.MemberGroupDO;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.tag.MemberTagDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.shop.controller.admin.member.vo.MemberUserDetailRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.member.vo.MemberUserUpdateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.member.vo.ShopMemberDetailRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.member.vo.ShopMemberSearchRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.order.vo.ShopOrderRespVO;
import cn.iocoder.yudao.module.shop.convert.memberhealth.ShopMemberHealthConvert;
import cn.iocoder.yudao.module.shop.convert.order.ShopOrderConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemtimes.ShopItemTimesDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberhealth.ShopMemberHealthDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberpay.ShopMemberPayDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberpay.ShopMemberPayItemDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.price.ShopPriceDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantdetail.TenantDetailDO;
import com.alipay.api.domain.ResourceDataVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

@Mapper
public interface ShopMemberUserConvert {
    ShopMemberUserConvert INSTANCE = Mappers.getMapper(ShopMemberUserConvert.class);

    default MemberUserDO convert(MemberUserUpdateReqVO bean){
        if(bean == null)
        {
            return null;
        }

        MemberUserDO.MemberUserDOBuilder memberUserDO = MemberUserDO.builder();

        memberUserDO.id(bean.getId());
        memberUserDO.tenantId(bean.getTenantId());
        memberUserDO.sex(bean.getSex());
        memberUserDO.mobile(bean.getMobile());
        memberUserDO.nickname(bean.getNickname());
        memberUserDO.birthday(bean.getBirthday());
        memberUserDO.mark(bean.getMark());

        return memberUserDO.build();
    }

    default PageResult<MemberUserDetailRespVO> convertPage(PageResult<MemberUserRespVO> pageResult,
                                                           List<PayWalletDO> wallets,
                                                           List<ShopMemberPayItemDO> memberpayItems,
                                                           List<ShopPriceDO> shopPriceList){

        PageResult<MemberUserDetailRespVO> result =  new PageResult<MemberUserDetailRespVO>();

        Map<Long, PayWalletDO> walletMap = convertMap(wallets, PayWalletDO::getUserId);
        Map<Long, ShopMemberPayItemDO> memberpayMap = convertMap(memberpayItems, ShopMemberPayItemDO::getMemberId);
        Map<Long, ShopPriceDO> shopPriceMap = convertMap(shopPriceList, ShopPriceDO::getId);

        List<MemberUserDetailRespVO> list = new ArrayList<>();
        for(MemberUserRespVO respVO: pageResult.getList()){
            MemberUserDetailRespVO detailRespVO = new MemberUserDetailRespVO();
            detailRespVO.setMember(respVO);

            // 余额
            if(walletMap.get(respVO.getId()) != null){
                detailRespVO.setBalance(walletMap.get(respVO.getId()).getBalance());
            }else{
                detailRespVO.setBalance(0);
            }

            // 每支价格
            if(memberpayMap.get(respVO.getId()) != null)
            {
                ShopMemberPayItemDO shopMemberPayItemDO = memberpayMap.get(respVO.getId());
                ShopPriceDO shopPriceDO = shopPriceMap.get(shopMemberPayItemDO.getPriceId());
                if(shopPriceDO == null){
                    detailRespVO.setPricePackageName("未找到");
                }else{
                    detailRespVO.setPricePackageName(shopPriceDO.getName());
                }
            }else {
                detailRespVO.setPricePackageName("未指定");
            }

            list.add(detailRespVO);
        }

        result.setList(list);
        result.setTotal(pageResult.getTotal());

        return result;
    }

    default ShopMemberDetailRespVO convertDetail(MemberUserDO memberUserDO, Integer balance, Integer totalRecharge, ShopMemberHealthDO healthDO, List<BlogJiuTagDO> tagList, List<ShopOrderRespVO> orderList){
        ShopMemberDetailRespVO respVO = new ShopMemberDetailRespVO();

        MemberUserRespVO memberUserRespVO = MemberUserConvert.INSTANCE.convert03(memberUserDO);
        respVO.setMemberInfo(memberUserRespVO);

        respVO.setBalance(balance);
        respVO.setTotalRecharge(totalRecharge);

        respVO.setHealthInfo(ShopMemberHealthConvert.INSTANCE.convert(healthDO, tagList));

        respVO.setOrderList(orderList);

        return respVO;
    }

    default List<ShopMemberSearchRespVO> convertSearchMember(List<MemberUserDO> memberUserDOList, List<TenantDetailDO> tenantDetailDOList){
        if(memberUserDOList.isEmpty()){
            return new ArrayList<>();
        }

        List<ShopMemberSearchRespVO> list = new ArrayList<>();
        Map<Long, TenantDetailDO> tenantMap = new HashMap<>();
        for(int i = 0; i<tenantDetailDOList.size(); i++){
            tenantMap.put(tenantDetailDOList.get(i).getTenantId(), tenantDetailDOList.get(i));
        }

        for(int i = 0; i<memberUserDOList.size(); i++){
            MemberUserDO memberUserDO = memberUserDOList.get(i);
            ShopMemberSearchRespVO respVO = new ShopMemberSearchRespVO();
            respVO.setId(memberUserDO.getId());
            respVO.setMobile(memberUserDO.getMobile());
            respVO.setNickname(memberUserDO.getNickname());
            respVO.setTenantId(memberUserDO.getTenantId());

            TenantDetailDO tenantDetailDO = tenantMap.get(memberUserDO.getTenantId());

            if(tenantDetailDO != null){
                respVO.setTenantName(tenantDetailDO.getName());
            }else{
                respVO.setTenantName("未找到店铺名");
            }

            list.add(respVO);
        }

        return list;
    }
}
